<template>
  <div>
    <el-card class="box-card" style="height: 850px">
      <!--            <el-card class="box-card">-->
      <el-row>
        <el-col :span="20">
          <el-form :inline="true" :model="queryData" class="demo-form-inline">
            <el-form-item>
              <el-input
                prefix-icon="el-icon-search"
                v-model="queryData.programTitle"
                placeholder="标题"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="queryList">查询</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addData">+ 添加小程序</el-button>
        </el-col>
      </el-row>
      <!--            </el-card>-->
      <div class="interspaceH"></div>
      <el-card class="box-card" style="height: 690px">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            prop="programId"
            label="小程序ID"
            width=""
          ></el-table-column>
          <el-table-column
            prop="programTitle"
            label="标题"
            width=""
          ></el-table-column>
          <el-table-column
            prop="programMessage"
            label="内容"
            width=""
          ></el-table-column>
          <el-table-column prop="programImage" label="图片" width="">
            <template slot-scope="scope">
              <el-image
                style="width: 50px; height: 50px"
                :src="imgFormate(scope.row.programImage)"
                fit="fill"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="programUrl"
            label="链接"
            width=""
          ></el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="details(scope.$index, scope.row)"
                >详情</el-button
              >
              <el-button type="text" @click="editor(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="del(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="queryDataHandle(pagePagination.currentPage, $event)"
          @current-change="queryDataHandle($event, pagePagination.pageSize)"
          :current-page="pagePagination.currentPage"
          :page-sizes="pagePagination.pageSizes"
          :page-size="pagePagination.pageSize"
          layout="total, sizes, prev, pager, next"
          :total="pagePagination.total"
        >
        </el-pagination>
      </el-card>
    </el-card>
    <!--添加-->
    <el-dialog
      title="添加小程序"
      :visible.sync="add"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="createData" :model="createData" label-width="70px">
        <el-form-item label="小程序ID" prop="programId">
          <el-input
            v-model="createData.programId"
            style="width: 200px"
            placeholder="请输入小程序ID"
          ></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="programTitle">
          <el-input
            v-model="createData.programTitle"
            style="width: 200px"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="programMessage">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="createData.programMessage"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="programImage">
          <el-upload
            class="avatar-uploader"
            ref="uploadAdd"
            :action="'http://localhost:8088/common/upload'"
            :show-file-list="false"
            :on-success="callbackSuccess"
            :on-error="errorAdd"
            accept="image/jpeg"
            :on-change="handleAvatarChange"
            :data="{ type: '3' }"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="createData.image"
              :src="createData.image"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接" prop="programUrl">
          <el-input
            v-model="createData.programUrl"
            style="width: 200px"
            placeholder="请输入链接存放路径"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="close">取消</el-button>
          <el-button type="primary" size="small" @click="submit"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--详情-->
    <el-dialog
      title="详情"
      :visible.sync="view"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="viewData" :model="viewData" label-width="70px" disabled>
        <el-form-item label="小程序ID" prop="programId">
          <el-input
            v-model="viewData.programId"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="viewData.programTitle"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="massage">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            v-model="viewData.programMessage"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <el-image
            v-if="viewData.programImage"
            :src="imgFormate(viewData.programImage)"
            class="avatar"
            :preview-src-list="programImageMax"
          ></el-image>
        </el-form-item>
        <el-form-item label="链接" prop="url">
          <el-input
            v-model="viewData.programUrl"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="programStatus">
          <el-input
            v-model="viewData.programStatus"
            style="width: 200px"
          ></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button size="small" @click="close">取消</el-button>
      </div>
    </el-dialog>
    <!--编辑-->
    <el-dialog
      title="编辑小程序"
      :visible.sync="edit"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="updateData" :model="updateData" label-width="70px">
        <el-form-item label="标题" prop="programTitle">
          <el-input
            v-model="updateData.programTitle"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="programMessage">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            v-model="updateData.programMessage"
          ></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="programImage">
          <el-upload
            class="avatar-uploader"
            ref="uploadUpdate"
            :action="'http://localhost:8088/common/upload'"
            :show-file-list="false"
            :on-success="callbackUpdate"
            :on-error="errorAdd"
            accept="image/jpeg"
            :on-change="handleAvatarChange2"
            :data="{ type: '3' }"
          >
            <img
              v-if="updateData.image"
              :src="imgFormate(updateData.image)"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="链接" prop="programUrl">
          <el-input
            v-model="updateData.programUrl"
            style="width: 200px"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="programUrl">
          <el-input
            v-model="updateData.programStatus"
            style="width: 200px"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="close">取消</el-button>
          <el-button type="primary" size="small" @click="save">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const initData = {
  programId: "",
  programTitle: "",
  programMessage: "",
  programImage: "",
  image: "",
  programUrl: "",
  programStatus: "",
};
export default {
  data() {
    return {
      add: false,
      view: false,
      edit: false,
      tableData: [],

      queryData: {
        programTitle: "",
      },
      createData: {},
      updateData: {},
      viewData: {},

      // 分页参数
      pagePagination: {
        currentPage: 1,
        pageSizes: [10, 15, 20, 25],
        pageSize: 10,
        total: 0,
      },
    };
  },
  mounted() {
    // this.queryDataHandle(1,10);
    this.queryList();
  },
  methods: {
    imgFormate(img) {
      return "http://localhost:8088/common/download/" + img;
    },
    //条件查询数据
    queryList() {
      let that = this;
      let payload = {
        programTitle: that.queryData.programTitle,
        pageNum: that.pagePagination.currentPage,
        pageSize: that.pagePagination.pageSize,
      };
      that.$request("/program/getByTitle", payload, "POST").then((data) => {
        that.tableData = data.data.data;
        that.pagePagination["total"] = data.data.total;
        that.pagePagination["pageSize"] = that.pagePagination.pageSize;
      });
    },
    //新建时修改图片信息时执行此方法
    handleAvatarChange(file) {
      if (this.beforeAvatarUpload(file.raw)) {
        this.createData.image = URL.createObjectURL(file.raw);
        this.createData.type = 1;
      }
    },
    //编辑时修改图片信息时执行此方法
    handleAvatarChange2(file) {
      if (this.beforeAvatarUpload(file.raw)) {
        this.updateData.image = URL.createObjectURL(file.raw);
        this.updateData.type = 1;
      }
    },
    //验证图片是否合规
    beforeAvatarUpload(file) {
      const isPNG = file.type === "image/png";
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isJPG && !isPNG) {
        this.$message.error("上传头像只能是图片格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isPNG || (isJPG && isLt2M);
    },
    //打开新建数据窗口
    addData() {
      this.createData = { ...initData };
      this.add = true;
    },
    //图片上传出错
    errorAdd(err, file, fileList) {
      this.$message({
        message: "图片上传失败，请检查",
        type: "error",
      });
    },
    //带图片信息的创建数据
    callbackSuccess(res, file) {
      console.log(res.data);
      this.createData.programImage = res.data;
    },
    //创建数据请求
    create() {
      this.$request("/program/add", this.createData, "POST").then((data) => {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.queryList();
      });
      this.close();
    },
    //修改带图片信息数据
    callbackUpdate(res, file) {
      this.updateData.programImage = res.data;
    //   this.update();
    },
    //修改数据请求
    update() {
      this.$request("/program/update", this.updateData, "PUT").then((data) => {
        this.$message({
          message: "修改成功 ！",
          type: "success",
        });
        this.queryList();
      });
      this.close();
    },
    //打开详情页面
    details(index, row) {
      this.viewData = row;
      this.view = true;
    },
    //打开修改页面
    editor(index, row) {
      row.image = row.programImage;
      this.updateData = { ...row };
      this.edit = true;
    },
    //提交修改数据
    save() {
      this.update();
    },
    //提交新增数据
    submit() {
      this.create();
    },
    //删除数据
    del(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$request(
            "/program/delete/" + row.programId,
            "",
            "DELETE"
          ).then((data) => {
            this.$message({
              message: "删除成功",
              type: "success",
            });
            this.queryList(row);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //关闭弹窗
    close() {
      this.add = false;
      this.view = false;
      this.edit = false;
    },

    // // 分页插件事件
    // queryDataHandle(page = 1, row = 10) {
    //     let that = this;
    //     let payload = {
    //         pageNum: page,
    //         pageSize: row
    //     }
    //     that.$request("/api/program/getByTitle/1_0", payload, "POST").then((data) => {
    //         that.tableData = data.rows
    //         that.pagePagination['total'] = data.total
    //         that.pagePagination['pageSize'] = row
    //     })
    // },
  },
  computed: {
    programImageMax() {
      return [this.viewData.programImage];
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
